﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Loong.Model;

namespace QYWebSiteBN
{
    /// <summary>
    /// 静态方式存储ContentTagInfo
    /// </summary>
    public class HTMLTag
    {
        private static List<ContentTagInfo> contentTagList = new List<ContentTagInfo>();

        private static HTMLTag htmlTag = null;

        private HTMLTag()
        {
            if (htmlTag == null)
            {
                htmlTag = new HTMLTag();
            
            }
        }

        /// <summary>
        /// 添加一个ContentTagInfo
        /// </summary>
        /// <param name="info"></param>
        public static void Add(ContentTagInfo info)
        {
            contentTagList.Add(info);
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="name">标签名</param>
        public static bool Exists(string name)
        {
            for (int i = 0; i < contentTagList.Count; i++)
            {
                if (contentTagList[i].Name.Trim().ToLower() == name.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 数量
        /// </summary>
        public static int Count
        {
            get { return contentTagList.Count; }
        }


        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static ContentTagInfo Get(int index)
        {
            return contentTagList[index];
        }


        public static void Edit(int index, ContentTagInfo info)
        {
           
            contentTagList[index] = info;
          
        }


        public static void Delete(int index)
        {
            contentTagList.RemoveAt(index);
        }

        public static void Clear()
        {
            contentTagList.Clear();
        }


        /// <summary>
        /// 从文件载入数据
        /// </summary>
        public static void Load(string filename)
        {
            Loong.Store.ObjectStoreQueue store = new Loong.Store.ObjectStoreQueue(filename);
            store.Load();
            for (int i = 0; i < store.Count; i++)
            {
                contentTagList.Add((ContentTagInfo)store.Dequeue());
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        public static void Save(string filename)
        {
            try
            {
                Loong.Store.ObjectStoreQueue store = new Loong.Store.ObjectStoreQueue(filename);
                foreach (ContentTagInfo info in contentTagList)
                {
                    store.Enqueue(info);
                }
            store.Save();          
            }
            catch(Exception e)
            {
                throw e;
            }         
        }

    }
}
